import { useRequest } from 'umi';
import { getCompanyID, getInvoiceCode, getKeyRouter, getPersonalInfo, getPosIDs, getTenantID, getUserDepID, getUserID, requestFunComponFiex } from '@/utils';
import { QC_SQSTATE } from '../common/Format';
import { ILoadMapTypeAll } from '@/common';
import { NeedMaterialTabSwitch } from '../mconfig';
import { getBaseUrl } from '@/services/urlconst';
import { useMemo } from 'react';
const baseUrl = getBaseUrl();
export const ApplyUpdateService = (): Record<string, any> => {
  /**查询所有项目 */
  const getProjectService = () => {
    const request = useRequest(
      (payload: Record<string, any>) => {
        let reqParams: Record<string, any> = {
          current: 1,
          pageSize: 10,
          sort: 'KSRQ',
          order: 'desc',
          BMDM: getUserDepID(),
          // GSDM: getCompanyID(),
          ...payload,
        };
        let reqUrl = `${baseUrl}/purchaseApplyController/getProject`;
        return requestFunComponFiex('POST', reqUrl, reqParams);
      },
      {
        // manual: true,
        paginated: true,
        defaultPageSize: 10,
      },
    );
    const ProjectData = useMemo(() => {
      // return []
      if (request?.data?.rows?.length) {
        const newData: Record<string, any>[] = [];
        request?.data?.rows?.forEach((item) => {
          newData.push({
            ...item,
            projectCode: item?.xmdm,
            projectName: item?.xmmc,
            projectSum: item?.ysje,
            mockID: getKeyRouter(),
          });
        });
        return newData;
      } else {
        return [];
      }
    }, [request?.data?.rows]);

    return {
      getProjectRun: request.run,
      getProjectLoading: request.loading,
      getProjectPagination: request.pagination,
      ProjectData,
    };
  };

  /**查询采购申请详情 */
  const findApplyService = () => {
    const request = useRequest(
      (payload: any) => {
        let reqUrl = `${baseUrl}/purchaseApplyController/getApplyDetails/${payload.id}/${getUserID()}`;
        return requestFunComponFiex('GET', reqUrl, {});
      },
      {
        manual: true,
      },
    );
    return {
      findApplyRun: request.run,
      findApplyLoading: request.loading,
    };
  };

  /**查询采购计划详情 */
  const findPlanService = () => {
    const request = useRequest(
      (payload: any) => {
        let reqUrl = `${baseUrl}/purchasePlanController/getPlanDetails/${payload.id}`;
        return requestFunComponFiex('GET', reqUrl, {});
      },
      {
        manual: true,
      },
    );
    return {
      findPlanRun: request.run,
      findPlanLoading: request.loading,
    };
  };

  /**添加采购申请 */
  const addApplyService = () => {
    const request = useRequest(
      (payload: any) => {
        let reqUrl = `${baseUrl}/purchaseApplyController/addApply`;
        let params = {
          ...payload,
          expectSum: 0,
          supplyKind: `${getTenantID()}${ILoadMapTypeAll?.PURCHASE_CATEGORY}1000`,
          businessType: `${getTenantID()}${ILoadMapTypeAll?.ALLFLOW_TYPE}1003`,
          state: QC_SQSTATE(),

          applyDepartmentID: getUserDepID(),
          applyerID: getUserID(),
          applyPositionID: getPosIDs(),
          applyCompanyID: getCompanyID(),

          createrID: getUserID(),
          createPositionID: getPosIDs(),
          createDepartmentID: getUserDepID(),
          createCompanyID: getCompanyID(),
        };
        return requestFunComponFiex('POST', reqUrl, params);
      },
      {
        manual: true,
      },
    );
    return {
      addApplyRun: request.run,
      addApplyLoading: request.loading,
    };
  };

  /** 编辑采购申请 */
  const editApplyService = () => {
    const request = useRequest(
      (payload: any) => {
        let reqUrl = `${baseUrl}/purchaseApplyController/editApply`;
        let params = {
          ...payload,
          applyPositionID: getPosIDs(),
          applyCompanyID: getCompanyID(),
          modifyerID: getUserID(),
          modifyPositionID: getPosIDs(),
          modifyDepartmentID: getUserDepID(),
          modifyCompanyID: getCompanyID(),
        };
        return requestFunComponFiex('PUT', reqUrl, params);
      },
      {
        manual: true,
      },
    );
    return {
      editApplyRun: request.run,
      editApplyLoading: request.loading,
    };
  };

  /**获取采购需求物料数据 */
  // TODO: 梁豪杰 待做权限判断
  const findNeedMaterialService = (Callback, curRes) => {
    const request = useRequest(
      (payload: any) => {
        let params = {
          current: 1,
          pageSize: 10,
          sort: 'createTime',
          order: 'desc',
          checkState: 1,
          completeFlag: 0,
          ...payload,
          ...NeedMaterialTabSwitch(curRes),
        };
        let reqUrl = `${baseUrl}/purchaseNeedMaterialController/dataGrid/${params?.params}`;
        return requestFunComponFiex('POST', reqUrl, params);
      },
      {
        manual: true,
        paginated: true,
        defaultPageSize: 10,
        onSuccess: Callback,
      },
    );
    return {
      findNeedMaterialRun: request.run,
      findNeedMaterialLoading: request.loading,
      findNeedMaterialDataSource: request?.data?.rows,
      findNeedMaterialProps: (request as any).tableProps,
    };
  };

  /**添加包数据 */
  const addPackService = () => {
    const request = useRequest(
      (payload: any) => {
        let reqUrl = `${baseUrl}/purchasePackController/addPack`;
        let params = {
          ...payload,
          createrID: getUserID(),
          createPositionID: getPosIDs(),
          createDepartmentID: getUserDepID(),
          createCompanyID: getCompanyID(),
        };
        return requestFunComponFiex('POST', reqUrl, params);
      },
      {
        manual: true,
      },
    );
    return {
      addPackRun: request.run,
      addPackLoading: request.loading,
    };
  };

  /**获取包数据 */
  const getPackService = () => {
    const request = useRequest(
      (payload: any) => {
        let reqUrl = `${baseUrl}/purchasePackController/getPackDetails`;
        let params = {
          ...payload,
          dicTypeIds: `${getTenantID()}${ILoadMapTypeAll?.MATERIAL_TYPE}`,
        };
        return requestFunComponFiex('POST', reqUrl, params);
      },
      {
        manual: true,
      },
    );
    return {
      getPackRun: request.run,
      getPackLoading: request.loading,
    };
  };

  /** 编辑标包 */
  const editPackServices = () => {
    const request = useRequest(
      (payload: any) => {
        let reqUrl = `${baseUrl}/purchasePackController/editPack`;
        let params = {
          ...payload,
          modifyerID: getUserID(),
          modifyPositionID: getPosIDs(),
          modifyDepartmentID: getUserDepID(),
          modifyCompanyID: getCompanyID(),
        };
        return requestFunComponFiex('POST', reqUrl, params);
      },
      {
        manual: true,
      },
    );

    return {
      editPackRun: request.run,
      editPackLoading: request.loading,
    };
  };

  return {
    findApplyService,
    findNeedMaterialService,
    findPlanService,
    addPackService,
    getPackService,
    getProjectService,
    addApplyService,
    editApplyService,
    editPackServices,
  };
};

/** 添加实施 */
export const addSourceDataServices = (cb) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
        ownPhone: getPersonalInfo()?.phoneMobile,
        // sourceCode: `HLZB00KCSS${moment().format('YYMM')}`,
      };

      let reqUrl = `${baseUrl}/purchaseSourceController/addSource`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );

  return {
    addSourceDataRun: request?.run,
    addSourceDataLoading: request?.loading,
  };
};

/** 修改计划状态,采购申请审批同意后/定标完成后-修改计划状态信息 */
export const updatePlanStateServices = (cb) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
        ownPhone: getPersonalInfo()?.phoneMobile,
      };

      let reqUrl = `${baseUrl}/purchasePlanController/updateState`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );

  return {
    updatePlanStateRun: request?.run,
    updatePlanStateLoading: request?.loading,
  };
};

/** 删除标包数据 */
export const deletPackService = () => {
  //cb: (res: Record<string, any>) => void
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${baseUrl}/purchasePackController/delPack`;
      return requestFunComponFiex('DELETE', reqUrl, bodyParams);
    },
    {
      manual: true,
      // onSuccess: cb,
    },
  );

  return {
    deletePackRun: request?.run,
    deletePackLoading: request?.loading,
  };
};

/** 获取规则 */
export const GetRuleSerivces = (Callback: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
      };

      // let reqUrl = `${baseUrl}/purchasePackController/findStyleRule`;
      let reqUrl = `${baseUrl}/purchasePackController/matchStyleRule`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );

  return {
    GetRuleRun: request?.run,
    GetRuleLoading: request?.loading,
  };
};

/** 修改申请是否 意向公开/招标采购代表 */
export const updateApplyFlagServices = (cb) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
      };

      let reqUrl = `${baseUrl}/purchaseApplyController/updateFlag`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );

  return {
    updateApplyFlagRun: request?.run,
    updateApplyFlagLoading: request?.loading,
  };
};

/** 提交意向公开 */
export const AddDisclosureServices = (cb) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
        // intentCode: `HLZB00KCYX${moment().format('YYMM')}`,
        intentCode: getInvoiceCode('YX'),
      };

      let reqUrl = `${baseUrl}/purchaseSourceIntentController/add`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );

  return {
    AddDisclosureRun: request?.run,
    AddDisclosureLoading: request?.loading,
  };
};

/** 编辑意向公开 */
export const EditDisclosureServices = (cb) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };

      let reqUrl = `${baseUrl}/purchaseSourceIntentController/edit`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res) => {
        cb(res);
      },
    },
  );

  return {
    EditDisclosureRun: request?.run,
    EditDisclosureLoading: request?.loading,
  };
};

/** 查看/获取意向公开 */
export const GetDetailDisclosureServices = (cb) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
      };

      let reqUrl = `${baseUrl}/purchaseSourceIntentController/getIntentById/${bodyParams?.id}/${getUserID()}`;
      return requestFunComponFiex('GET', reqUrl);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );

  return {
    GetDetailDisclosureRun: request?.run,
    GetDetailDisclosureLoading: request?.loading,
  };
};

/** 实施流标 - 锁定接口 */
export const AddSourceLockServices = (cb) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = { ...payload };
      let reqUrl = `${baseUrl}/purchaseSourceController/addLock`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );

  return {
    AddSourceLockRun: request?.run,
    AddSourceLockLoading: request?.loading,
  };
};

/** 实施流标 - 修改采购分包是否变更状态接口 */
export const updatePackFlagServices = (cb) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
      };

      let reqUrl = `${baseUrl}/purchasePackController/updateFlag`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );

  return {
    updatePackFlagRun: request?.run,
    updatePackFlagLoading: request?.loading,
  };
};

/** 实施流标 - 添加一条实施（待编制）数据 */
export const AddfailureChangeServices = (cb) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
        // sourceCode: `HLZB00KCSS${moment().format('YYMM')}`,
        sourceCode: getInvoiceCode('SS'),
      };

      let reqUrl = `${baseUrl}/purchaseSourceController/failureChange`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );

  return {
    AddfailureChangeRun: request?.run,
    AddfailureChangeLoading: request?.loading,
  };
};

/** 添加采购变更申请单 */
export const AddPurchasingMethodChangeServices = (cb) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
        // businessType: '100199031002',
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
        // changeCode: `HLZB00KCBG${moment().format('YYMM')}`,
      };

      let reqUrl = `${baseUrl}/purchaseSourceChangeController/add`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );

  return {
    AddPurchasingMethodChangeRun: request?.run,
    AddPurchasingMethodChangeLoading: request?.loading,
  };
};

/** 获取采购变更申请单 */
export const GetPurchasingMethodChangeServices = (cb) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
      };

      let reqUrl = `${baseUrl}/purchaseSourceChangeController/getSourceChangeById/${bodyParams?.id}/${getUserID()}`;
      return requestFunComponFiex('GET', reqUrl);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );

  return {
    GetPurchasingMethodChangeRun: request?.run,
    GetPurchasingMethodChangeLoading: request?.loading,
  };
};

/** 编辑采购变更申请单 */
export const EditPurchasingMethodChangeServices = (cb) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };

      let reqUrl = `${baseUrl}/purchaseSourceChangeController/edit`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );

  return {
    EditPurchasingMethodChangeRun: request?.run,
    EditPurchasingMethodChangeLoading: request?.loading,
  };
};

/** 改变标包拟采方式Services */
export const editPackProcureRealServices = (cb) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };

      let reqUrl = `${baseUrl}/purchasePackController/updateState`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );

  return {
    editPackProcureRealChangeRun: request?.run,
    editPackProcureRealChangeLoading: request?.loading,
  };
};

/** 改变变更采购申请工单状态的services */
export const alterApplyChangeStateServices = (cb) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
        // modifyerID: getUserID(),
        // modifyPositionID: getPosIDs(),
        // modifyDepartmentID: getUserDepID(),
        // modifyCompanyID: getCompanyID(),
      };

      let reqUrl = `${baseUrl}/purchaseSourceChangeController/updateState`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );

  return {
    alterApplyChangeStateChangeRun: request?.run,
    alterApplyChangeStateLoading: request?.loading,
  };
};

/** 变更实施状态 */
export const setSourceStateService = (cb) => {
  const request = useRequest(
    (payload: Record<string, any>) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${baseUrl}/purchaseSourceController/editState`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );

  return {
    setSourceStateRun: request?.run,
    setSourceStateLoading: request?.loading,
  };
};

/** 查询当前申请下是否有标包在走变更申请流程 */
export const inspectChangeStateService = (cb) => {
  const request = useRequest(
    (payload: Record<string, any>) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${baseUrl}/purchasePackController/inspectChangeState`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );

  return {
    inspectChangeStateRun: request?.run,
    inspectChangeStateLoading: request?.loading,
  };
};

/**同步PDF【采购申请】*/
export const syncPDFtoOAServices = (cb: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (payload) => {
      let reqParams = { ...payload };
      let reqUrl = `${baseUrl}/purchaseApplyController/syncWorkFlow`;
      return requestFunComponFiex('POST', reqUrl, reqParams);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );

  return {
    syncPDFtoOARun: request?.run,
    syncPDFtoOALoading: request?.loading,
  };
};

/**同步PDF【变更采购方式-线下】*/
export const syncPDFtoOAMethodChangeServices = (cb: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (payload) => {
      let reqParams = { ...payload };
      let reqUrl = `${baseUrl}/purchaseSourceChangeController/syncWorkFlow`;
      return requestFunComponFiex('POST', reqUrl, reqParams);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );

  return {
    syncPDFtoOARun: request?.run,
    syncPDFtoOALoading: request?.loading,
  };
};
